"use strict";
const semver = require("semver"); //这个库是用来对比版本号的
const colors = require("colors"); //改变控制台输出的颜色
const log = require("@hjc-cli-dev/log"); //改变控制台输出的颜色
const LOWEST_NODE_VERSION = "12.0.0";

class Command {
    constructor(argv) {
        if (!argv) {
            throw new Error("参数必须传入！");
        }
        if (!Array.isArray(argv)) {
            throw new Error("参数必须得是一个数组！");
        }
        if (argv.length < 1) {
            throw new Error("参数列表为空！");
        }
        this._argv = argv;
        let runner = new Promise((res, rej) => {
            let chain = Promise.resolve();
            chain = chain.then(() => {
                this.checkNodeVersion();
            });
            chain = chain.then(() => {
                this.initArgv();
            });
            chain = chain.then(() => {
                this.init();
            });
            chain = chain.then(() => {
                this.exec();
            });
            chain.catch((err) => {
                log.error(err.message);
            });
        });
    }
    initArgv() {
        this._cmd = this._argv[this._argv.length - 1];
        this._argv = this._argv.slice(0, this._argv.length - 1);
    }
    checkNodeVersion() {
        const current = process.version;
        if (!semver.gte(current, LOWEST_NODE_VERSION)) {
            //比对本地node和限制使用的node，大于等于才为true
            throw new Error(colors.red(`hjc-cli 需要安装 v${LOWEST_NODE_VERSION}以上版本的node.js`));
        }
    }
    init() {
        throw new Error("init必须实现！");
    }
    exec() {
        throw new Error("exec必须实现！");
    }
}
module.exports = Command;
